package com.maixun.lib_base.ui.activity

import android.content.Context
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Bundle
import android.view.View
import android.view.inputmethod.InputMethodManager
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import com.maixun.lib_base.R
import com.maixun.lib_base.utils.StatusBarUtil

/**
 * 作者：Lmoumou
 * 时间：2020/7/15 13:14
 */
abstract class BaseActivityImpl : AppCompatActivity(),
    BaseActivity {

    private var isImmerse = false

    override fun onCreate(savedInstanceState: Bundle?) {
        setStatusBarImmerse()
        super.onCreate(savedInstanceState)
        if (getLayoutId() != 0) {
            setContentView(getLayoutId())
        }
        val mToolbar: Toolbar? = findViewById(R.id.mTitleToolbar)
        initTitleBar(mToolbar)
        initView(savedInstanceState)
    }

    /**
     * 状态栏是否沉浸，如果沉浸需要状态栏透明
     *
     * @param isImmerse
     * */
    override fun setStatusBarImmerse(isImmerse: Boolean) {
        this.isImmerse = isImmerse
        if (isImmerse) {
            StatusBarUtil.setTranslucentStatus(this)
        }
    }

    /**
     * 设置状态颜色和样式
     *
     * @param dark 状态栏深色样式
     * @param color 状态栏颜色
     * */
    override fun setStatusBarStyle(dark: Boolean, color: Int) {
        if (isImmerse) {
            StatusBarUtil.setTranslucentStatus(this)
        } else {
            StatusBarUtil.setStatusBarColor(this, color)
        }
        StatusBarUtil.setStatusBarDarkTheme(this, dark)
    }

    /**
     * 初始化标题栏
     *
     * @param mToolbar
     * */
    override fun initTitleBar(mToolbar: Toolbar?) {
        mToolbar?.apply {
            title = ""
            this@BaseActivityImpl.setSupportActionBar(this)
            this@BaseActivityImpl.supportActionBar?.apply {
                setHomeButtonEnabled(true)
                setDisplayHomeAsUpEnabled(true)
            }
            this.setNavigationOnClickListener {
                this@BaseActivityImpl.onBackPressed()
            }
        }
    }

    /**
     * 打开软键盘
     *
     * @param view
     * */
    override fun openKeyBord(view: View) {
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(view, 0)
    }

    /**
     * 关闭软键盘
     *
     * @param view
     * */
    override fun closeKeyBord(view: View?) {
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        if (view == null) {
            val v = window.peekDecorView()
            if (v != null) {
                imm.hideSoftInputFromWindow(v.windowToken, 0)
            }
        } else {
            imm.hideSoftInputFromWindow(
                view.windowToken,
                0
            )
        }
    }

    override fun registerRxBus() {

    }

    /**
     * 字体不受系统改变
     * */
    override fun getResources(): Resources {
        val res = super.getResources()
        val config = Configuration()
        config.setToDefaults()
        this.createConfigurationContext(config)
        return res
    }
}